package crud.service;

import crud.bean.Employee;
import crud.bean.EmployeeExample;
import crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * EmployeeService
 *
 * @author Yin Guiqing
 */
@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    public List<Employee> getAllEmployees() {
        EmployeeExample example = new EmployeeExample();
        example.setOrderByClause("`emp_id` ASC");
        return employeeMapper.selectByExampleWithDept(example);
    }

    public void saveEmployee(Employee employee) {
        int i = employeeMapper.insertSelective(employee);
    }

    public boolean checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count == 0;
    }

    public Employee getEmpById(Integer empId) {
        return employeeMapper.selectByPrimaryKey(empId);
    }

    public int updateEmployee(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public int deleteById(Integer empId) {
        return employeeMapper.deleteByPrimaryKey(empId);
    }

    public int deleteBatch(List<Integer> listEmpId) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdIn(listEmpId);
        return employeeMapper.deleteByExample(example);
    }
}
